package cn.com.fuhuitong.main.home.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import cn.com.fuhuitong.R
import cn.com.fuhuitong.app.getImageUrl
import cn.com.fuhuitong.base.BaseDelegateAdapter
import cn.com.fuhuitong.main.ItemViewType
import cn.com.fuhuitong.main.base.RecyclerViewHolder
import cn.com.fuhuitong.main.home.entity.CaseEntity
import cn.com.fuhuitong.main.home.entity.CategoryTwoList
import com.alibaba.android.vlayout.LayoutHelper
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.TimeUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import kotlinx.android.synthetic.main.item_case_list_image.view.*
import kotlinx.android.synthetic.main.item_case_list_image_content.view.*
import kotlinx.android.synthetic.main.item_case_list_video.view.*
import kotlinx.android.synthetic.main.item_category_title.view.*
import kotlinx.android.synthetic.main.item_tab_layout.view.*
import java.lang.Exception


class CategoryTitleAdapter(
    private val mContext: Context,
    mHelper: LayoutHelper
) : BaseDelegateAdapter<String>(
    mContext = mContext,
    mHelper = mHelper,
    mLayoutId = R.layout.item_tab_layout,
    mItemViewType = ItemViewType.CASE_TAB
) {

    var selectedPosition = 0
        set(value) {
            if (field != value) {
                field = value
                notifyDataSetChanged()
            }
        }

    override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
        super.onBindViewHolder(holder, position)
        holder.itemView.text_tab_layout_name.text = data[position]
        holder.itemView.text_tab_layout_name.setTextColor(
            mContext.resources.getColor(
                if (holder.layoutPosition == selectedPosition) R.color.colorMain else R.color.colorMainGrey
            )
        )
        holder.itemView.isSelected = holder.layoutPosition == selectedPosition
    }
}


class CategoryListAdapter(
    private val mContext: Context, mHelper: LayoutHelper
) : BaseDelegateAdapter<CaseEntity>(
    mContext = mContext,
    mHelper = mHelper,
    mLayoutId = 0,
    mItemViewType = 0
) {

    init {
        addChildClickViewIds(R.id.text_case_list_video_play)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder {
        if (viewType == 1) {
            return RecyclerViewHolder(
                LayoutInflater.from(mContext)
                    .inflate(R.layout.item_case_list_video, parent, (false))
            )
        } else if (viewType == 2) {
            return RecyclerViewHolder(
                LayoutInflater.from(mContext)
                    .inflate(R.layout.item_case_list_image, parent, (false))
            )
        } else throw Exception("BaseDelegateAdapter onCreateViewHolder fail!")
    }

    override fun onBindViewHolder(itemView: View, position: Int, itemData: CaseEntity) {
        super.onBindViewHolder(itemView, position, itemData)

        if (getItemViewType(position) == 1) {
            Glide.with(itemView).load(getImageUrl(itemData.head_image))
                .apply(RequestOptions.bitmapTransform(CircleCrop()))
                .into(itemView.image_case_list_video_heads)

            Glide.with(itemView).load(getImageUrl(itemData.image))
                .into(itemView.image_case_list_video_icon)

            itemView.text_case_list_video_name.text = itemData.send_name
            itemView.text_case_list_video_time.text =
                TimeUtils.millis2String(itemData.createtime * 1000, "yyyy-MM-dd")
            itemView.text_case_list_video_title.text = itemData.title
        } else {
            Glide.with(itemView).load(getImageUrl(itemData.head_image))
                .apply(RequestOptions.bitmapTransform(CircleCrop()))
                .into(itemView.image_case_list_image_heads)

            itemView.text_case_list_image_name.text = itemData.send_name
            itemView.text_case_list_image_time.text =
                TimeUtils.millis2String(itemData.createtime * 1000, "yyyy-MM-dd")
            itemView.text_case_list_image_title.text = itemData.title

            itemView.recycler_case_list_image.layoutManager =
                GridLayoutManager(mContext, 3).apply {
                }
            val images: List<String> = itemData.images.split(",")
            itemView.recycler_case_list_image.adapter =
                ImageAdapter(mContext, images)
        }
    }

    override fun getItemViewType(position: Int): Int {
        return data[position].type
    }
}

private class ImageAdapter(
    private val mContext: Context,
    private val images: List<String>
) : RecyclerView.Adapter<RecyclerViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder {
        return RecyclerViewHolder(
            LayoutInflater.from(mContext)
                .inflate(R.layout.item_case_list_image_content, parent, (false))
        )
    }

    override fun getItemCount(): Int =
        if (images.size > 9) 9 else images.size

    override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
        Glide.with(holder.itemView).load(getImageUrl(images[position]))
            .into(holder.itemView.image_case_list_image_icon)
    }

}